<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\MenuController;
use App\Http\Controllers\DepartmentController;
use App\Http\Controllers\OperationLogController;
use App\Http\Controllers\OnlineUserController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\UploadController;
use App\Http\Controllers\LoginLogController;
use App\Http\Controllers\SystemController;
use App\Http\Controllers\StorageConfigController;
use App\Http\Controllers\WebSocketConfigController;
use App\Http\Controllers\EnvConfigController;

// 测试路由
Route::post('/test', function () {
    return response()->json([
        'code' => 200,
        'message' => '测试成功',
        'data' => ['test' => 'ok']
    ]);
});

// 公开路由
Route::post('/login', [AuthController::class, 'login'])->middleware('rate.limit:login,5,15');
Route::post('/refresh', [AuthController::class, 'refresh'])->middleware(['auth.api', 'rate.limit:refresh,10,1']);
Route::post('/forgot-password', [AuthController::class, 'forgotPassword']);
Route::post('/reset-password', [AuthController::class, 'resetPassword']);

// WebSocket配置（公开接口，前端需要知道是否启用WebSocket）
Route::get('/websocket/config', function () {
    return response()->json([
        'code' => 200,
        'message' => '获取成功',
        'data' => [
            'enabled' => config('websocket.enabled', false),
            'ws_url' => config('websocket.enabled', false) ? config('websocket.ws_url', 'ws://localhost:8080') : null,
        ],
    ]);
});

// 需要认证的路由
Route::middleware(['auth.api', 'rate.limit:api,60,1'])->group(function () {
    // 认证相关
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::get('/me', [AuthController::class, 'me']);
    
    // 个人中心
    Route::prefix('profile')->group(function () {
        Route::get('/', [ProfileController::class, 'show']);
        Route::put('/', [ProfileController::class, 'update']);
        Route::post('/password', [ProfileController::class, 'changePassword']);
        Route::post('/avatar', [ProfileController::class, 'uploadAvatar']);
    });

    // 用户管理
    Route::apiResource('users', UserController::class);
    Route::post('/users/{user}/force-logout', [UserController::class, 'forceLogout']);
    Route::post('/users/batch-destroy', [UserController::class, 'batchDestroy']);
    Route::post('/users/batch-update-status', [UserController::class, 'batchUpdateStatus']);
    Route::get('/users/export/list', [UserController::class, 'export']);
    Route::post('/users/import', [UserController::class, 'import']);
    Route::get('/users/{user}/permissions', [\App\Http\Controllers\UserPermissionController::class, 'show']);

    // 角色管理
    Route::apiResource('roles', RoleController::class);
    Route::get('/roles/all/list', [RoleController::class, 'all']);

    // 菜单管理
    Route::apiResource('menus', MenuController::class);
    Route::get('/menus/routes/list', [MenuController::class, 'routes']);
    
    // 权限管理
    Route::apiResource('permissions', PermissionController::class);
    Route::get('/permissions/all/list', [PermissionController::class, 'all']);

    // 部门管理
    Route::apiResource('departments', DepartmentController::class);

    // 操作日志
    Route::apiResource('operation-logs', OperationLogController::class);
    Route::delete('/operation-logs/clear/all', [OperationLogController::class, 'clear']);
    
    // 登录日志
    Route::apiResource('login-logs', LoginLogController::class);
    Route::delete('/login-logs/clear/all', [LoginLogController::class, 'clear']);

    // 在线用户
    Route::get('/online-users', [OnlineUserController::class, 'index']);
    Route::post('/online-users/{onlineUser}/force-logout', [OnlineUserController::class, 'forceLogout']);
    
    // 文件上传
    Route::post('/upload/image', [UploadController::class, 'image']);
    Route::post('/upload/file', [UploadController::class, 'file']);
    Route::delete('/upload/delete', [UploadController::class, 'delete']);
    
    // 系统管理
    Route::prefix('system')->group(function () {
        Route::get('/info', [SystemController::class, 'info']);
        Route::get('/statistics', [SystemController::class, 'statistics']);
        Route::post('/clear-cache', [SystemController::class, 'clearCache']);
    });
    
    // 存储配置管理（单配置模式）
    Route::get('/storage-config', [StorageConfigController::class, 'index']);
    Route::put('/storage-config', [StorageConfigController::class, 'update']);
    Route::post('/storage-config/test', [StorageConfigController::class, 'test']);
    
    // 兼容旧的复数路由（重定向到单数路由）
    Route::get('/storage-configs', [StorageConfigController::class, 'index']);
    Route::put('/storage-configs', [StorageConfigController::class, 'update']);
    Route::post('/storage-configs/test', [StorageConfigController::class, 'test']);
    
    // WebSocket 配置管理
    Route::get('/websocket-config', [WebSocketConfigController::class, 'index']);
    Route::put('/websocket-config', [WebSocketConfigController::class, 'update']);
    Route::post('/websocket-config/refresh', [WebSocketConfigController::class, 'refresh']);
    Route::post('/websocket-config/clear-cache', [WebSocketConfigController::class, 'clearCache']);
    
    // 环境变量配置管理
    Route::get('/env-config', [EnvConfigController::class, 'index']);
    Route::get('/env-config/all', [EnvConfigController::class, 'all']);
    Route::post('/env-config', [EnvConfigController::class, 'store']);
    Route::put('/env-config', [EnvConfigController::class, 'update']);
    Route::delete('/env-config', [EnvConfigController::class, 'destroy']);
    Route::post('/env-config/delete', [EnvConfigController::class, 'deleteEnv']);
    Route::post('/env-config/refresh', [EnvConfigController::class, 'refresh']);
    Route::post('/env-config/clear-cache', [EnvConfigController::class, 'clearCache']);
});

